package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.DcpgSurveyCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgSurveyResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.DcpgSurveyUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.DcpgSurveyEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface DcpgSurveyConvert {
    DcpgSurveyConvert INSTANCE = Mappers.getMapper(DcpgSurveyConvert.class);

    DcpgSurveyResultDTO convertTo(DcpgSurveyEntity s);

    DcpgSurveyEntity createTo(DcpgSurveyCreateDTO s);

    DcpgSurveyEntity updateTo(DcpgSurveyUpdateDTO s);

    default DcpgSurveyEntity create(DcpgSurveyCreateDTO s, Long tenantId) {
        DcpgSurveyEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        additionalArea(entity, s.getResidenceAreaList());
        return entity;
    }

    default DcpgSurveyEntity update(DcpgSurveyUpdateDTO s) {
        DcpgSurveyEntity entity = updateTo(s);
        additionalArea(entity, s.getResidenceAreaList());
        return entity;
    }

    default DcpgSurveyResultDTO convert(DcpgSurveyEntity s) {
        DcpgSurveyResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(DcpgSurveyEntity s, List<String> residenceAreaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(residenceAreaList)) {
            return;
        }
        s.setBdcrszs(residenceAreaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setBdcrszds(residenceAreaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (residenceAreaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setBdcrszxq(residenceAreaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setBdcrszxq(null);
        }

        if (residenceAreaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setBdcrszd(residenceAreaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setBdcrszd(null);
        }
    }

    default void renderAreaList(DcpgSurveyResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getBdcrszs()), () -> residenceAreaList.add(s.getBdcrszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getBdcrszds()), () -> residenceAreaList.add(s.getBdcrszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getBdcrszxq()), () -> residenceAreaList.add(s.getBdcrszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getBdcrszd()), () -> residenceAreaList.add(s.getBdcrszd()));

        s.setResidenceAreaList(residenceAreaList);
    }


}
